clear

orig_data=load('halo_MF_raw.dat');


% data is output per galaxy, we only want one result per halo
ids=unique(orig_data(:,1));

for i=1:size(ids)(1)
data(i,1)=orig_data(find(orig_data(:,1)==ids(i)),2)(1);
end

data=log10(data*1E10);

nbins=10;

count=zeros(nbins,1);

m_min=min(data);
m_max=max(data);
m_range=m_max-m_min;

% creates nbins+1 bin boundaries
m_range=[m_min:(m_max-m_min)/nbins:m_max]';

for i=1:nbins
% find the center of each bin
m_bins(i)=(m_range(i)+m_range(i+1))/2;
% need to get the bin size so we can normalize later
binsize(i)=abs(m_range(i+1)-m_range(i));
binsize=binsize';
end

m_bins=m_bins';

for i=1:size(data)(1)

diff=abs(m_bins-data(i,1));

a=find(diff==min(diff))(1);
% round into bins
count(a)=count(a)+1;

end

count=count/(62.5^3);

%note that we are still outputting mass in log10
out=[m_bins,log10(count./binsize)];

save -ascii 'halo_MF_output.dat' out

